package com.mall.server.controller.yxcontroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.FlashProductRelationService;
import com.tanhua.model.domain.SmsFlashPromotionProductRelation;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/flashProductRelation")
public class FlashProductRelationController {
    @Autowired
    private FlashProductRelationService flashProductRelationService;

    /**
     * 分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R list(Integer flashPromotionId,Integer flashPromotionSessionId,
                  @RequestParam(defaultValue = "1")Integer pageNum,
                  @RequestParam(defaultValue = "5")Integer pageSize){
        if (flashPromotionId == null  || flashPromotionSessionId==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }

        Page<SmsFlashPromotionProductRelation> page = new Page<>();
        PageVo pageVo =flashProductRelationService.list(flashPromotionId,flashPromotionSessionId,page);

        return R.success(pageVo);
    }

    /**
     * 修改关联信息
     * @param id
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateFlashProductRelation(@PathVariable("id")Integer id,@RequestBody SmsFlashPromotionProductRelation smsFlashPromotionProductRelation){

        if (id == null || smsFlashPromotionProductRelation == null){
            throw new BusinessException(ErrorResult.parameterError());
        }

        flashProductRelationService.updateFlashProductRelation(smsFlashPromotionProductRelation);
        return R.success("");
    }

    /**
     * 删除关联
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteFlashProductRelation(@PathVariable("id") Long id){
        if (id == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashProductRelationService.deleteFlashProductRelation(id);
        return R.success("");
    }


    /**
     * 批量选择商品添加关联
     * @param relationList
     * @return
     */
    @PostMapping("/create")
    public R createFlashProductRelation(@RequestBody List<SmsFlashPromotionProductRelation> relationList){
        if (relationList == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashProductRelationService.createFlashProductRelation(relationList);
        return R.success("");
    }

}
